﻿using CommunityToolkit.Mvvm;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using DevExpress.Xpf.Charts;
using HandyControl.Data;
using MeasureAbstract.IMotionDevice;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Forms;
using System.Windows.Markup;
using System.Windows.Media;
using 立式传动转台控制系统;
using 长屏_ARM_STM32F429控制板;


namespace 三维转台控制系统
{
    public partial class MainWindowViewModel : ObservableObject
    {
        public SystemConfig SystemConfig { get; set; } = SystemManage.SystemConfig;
        System.Timers.Timer Timer;
        public MainWindowViewModel()
        {
            Timer=new System.Timers.Timer();
            Timer.Interval = 200;
            Timer.Elapsed += Timer_Elapsed;
            Timer.Start();


            //
            ChartControl= new ChartControl();
            var xy = new XYDiagram2D();
            xy.DefaultPane=new Pane() { DomainBorderBrush=Brushes.Transparent,DomainBrush=Brushes.Transparent};
            xy.AxisX = new AxisX2D() { Label = new AxisLabel() { Foreground = Brushes.White } };
            xy.AxisY = new AxisY2D() { Label = new AxisLabel() { Foreground = Brushes.White }, Interlaced = false };

            ChartControl.Diagram = xy;
            ChartControl.Background=Brushes.Transparent;
            ChartControl.Foreground = Brushes.Transparent;
            ChartControl.BorderBrush=Brushes.Transparent;
            ////添加纵轴标题
            //ChartControl.Titles.Add(new Title()
            //{
            //    Content = "对\n数\n幅\n度",
            //    FontSize = 14,
            //    VerticalAlignment = VerticalAlignment.Center,
            //    Margin = new Thickness(0, 0, 5, 0),
            //    Dock = System.Windows.Controls.Dock.Left
            //});
        }

        double S;
        private void Timer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e)
        {
            for(int i = 0;i<SystemConfig.Axes.Count;i++)
            {
                SystemConfig.Axes[i].CurrentPosition = Math.Sin(S * Math.PI / 18);
                SystemConfig.Axes[i].CurrentSpeed = Math.Cos(S*Math.PI/18);
                SystemConfig.Axes[i].CurrentAcceleration = Math.Cos((S+90) * Math.PI / 18);
                System.Windows.Application.Current.Dispatcher.Invoke(() => 
                {
                    SystemConfig.Axes[i].AddPoint(new MoveState() { Time = DateTime.Now.ToString("ssfff"), Position = SystemConfig.Axes[i].CurrentPosition, Speed = SystemConfig.Axes[i].CurrentSpeed, Acceleration = SystemConfig.Axes[i].CurrentAcceleration });
                });
                    
                
            }
            S+=1;
        }


        [ObservableProperty]
        private string currentFile = ".\\动画\\星球.json";

        [ObservableProperty]
        private string log = DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss：")+"系统启动完成\n"+ DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss：") + "系统关闭";

        [ObservableProperty]
        private string exceptionLog = DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss：") + "方位轴掉线";

        [ObservableProperty]
        private bool connectState = true;


        [ObservableProperty]
        private CommunicationConfig config = new CommunicationConfig()
        {
            ShowTCP = true,
            ShowUDP = false,
            ShowGPIB = false,
            ShowUART = false,
            CommunicationList=new ObservableCollection<CommunicationMode>()
            {
                new CommunicationMode() { Name = "TCP", Selected = true, Support = true },
                new CommunicationMode() {Name="UDP", Support = true},
                new CommunicationMode() {Name="UART", Support = true},
                new CommunicationMode() {Name="GPIB", Support = true},

            },
        };


        public ChartControl ChartControl{ get; set; }














        /// <summary>  
        /// 由结构体转换为byte数组  
        /// </summary>  
        public byte[] StructureToByte<T>(T structure)
        {
            int size = Marshal.SizeOf(typeof(T));
            byte[] buffer = new byte[size];
            IntPtr bufferIntPtr = Marshal.AllocHGlobal(size);
            try
            {
                Marshal.StructureToPtr(structure, bufferIntPtr, true);
                Marshal.Copy(bufferIntPtr, buffer, 0, size);
            }
            finally
            {
                Marshal.FreeHGlobal(bufferIntPtr);
            }
            return buffer;
        }

        /// <summary>  
        /// 由byte数组转换为结构体  
        /// </summary>  
        public T ByteToStructure<T>(byte[] dataBuffer)
        {
            object structure = null;
            int size = Marshal.SizeOf(typeof(T));
            IntPtr allocIntPtr = Marshal.AllocHGlobal(size);
            try
            {
                Marshal.Copy(dataBuffer, 0, allocIntPtr, size);
                structure = Marshal.PtrToStructure(allocIntPtr, typeof(T));
            }
            finally
            {
                Marshal.FreeHGlobal(allocIntPtr);
            }
            return (T)structure;
        }

        

       
    }

    
   


   
    
}
